<?php
	/**
	 * 控制类
	 * detail description
	 * @todo
	 * @author swingcoder <swingcoder@msn.com>
	 * @version 0.1
	 * @copyright Copyright (c) 2009, swingcoder.com
	 */

	require_once 'base.php';

	class controller extends base
	{

		final public function run()
		{
			$this->control_init();

			$this->format();

			$method = strtolower( $this->server['REQUEST_METHOD'] );

			if( in_array( $method, array('get','post') ) )
			{
				if( $this->check( $method ) )
				{
					$do = 'do_' . $method;
					$this->$do();
				}
				//else
				//{
				//	trigger_error("CHECK FAILURE.", E_USER_ERROR);
				//}
			}
			else
			{
				trigger_error("UNSUPPORT HTTP METHOD.", E_USER_ERROR);
			}
		}

		final private function control_init()
		{
			ob_start( 'ob_gzhandler' );
			ini_set( 'date.timezone','Asia/Shanghai' );
			ini_set( 'display_errors', true );
			error_reporting( E_ALL );
			set_error_handler( array( $this, 'error' )  );
			ignore_user_abort( true );
			header( 'Content-type: text/html; charset=utf-8' );
		}

		public function check( $method )
		{
			$check = 'check_' . $method;
			return $this->$check() && $this->check_privileges();
		}

		function check_privileges()
		{
			return true;
		}

		public function check_get()
		{
			return true;
		}

		public function check_post()
		{
			return true;
		}

		public function do_get()
		{
			echo __METHOD__;
		}

		public function do_post()
		{
			echo __METHOD__;
		}

		protected function format()
		{
			array_walk_recursive( $_GET, array( $this, 'parsestr' ) );
			array_walk_recursive( $_POST, array( $this, 'parsestr' ) );
			//array_walk_recursive( $_COOKIE, array( $this, 'parsestr' ) );
			//array_walk_recursive( $_SESSION, array( $this, 'parsestr' ) );
		}

		private function parsestr( &$item, $key )
		{
			$item = trim( $item );
			$item = htmlspecialchars( $item, ENT_QUOTES );
		}

		public function error($no, $str, $file, $line)
		{
			$errmsg = '<font color="red">Runtime Error:</font> ' . $str . ' file ' . $file . '  on line ' . $line . '<br />';

			if ( $this->_config['env']['debug'] == true )
			{
				echo $errmsg;
				//$this->msg( $errmsg );	//不能再引用模板，会导致嵌套
			}
			else
			{
				error_log( '[' . date('Y-m-d H:i:s') . '] ' . $errmsg . "\r\n", 3, $this->_config['file']['errorlog']);
			}

			//return true;
			exit(1);
		}

		public function msg( $msg, $timeout=-1, $back_url='' )
		{
			if( empty($back_url) )
			{
				$back_url = empty($this->server['HTTP_REFERER']) ? 'javascript:history.back();' : $this->server['HTTP_REFERER'];
			}

			if( $timeout < 0 )
			{
				$redirect = '';
				$back_msg = ' <a href="' . $back_url . '">请点击返回</a>';
			}
			else
			{
				$redirect = '<meta http-equiv="refresh" content="' . $timeout . '; url=' . $back_url . '">';
				$back_msg = ' <a href="' . $back_url . '">请点击返回</a> 或 系统将' . ($timeout == 0 ? '' : $timeout . ' 秒后') . '自动跳转。';
			}

			$data = array( 'redirect'=>$redirect, 'msg'=>$msg, 'back_msg'=>$back_msg );

			$this->view('msg', $data);
		}

		public function __destruct()
		{
			foreach( $this->load_object as $obj )
			{
				unset( $obj );
			}
		}

	}
?>